package com.runen.infinitemovement.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PersonChallengeActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.v_title_line)
    View vTitleLine;
    @BindView(R.id.ll_freedom_challenge)
    LinearLayout llFreedomChallenge;
    @BindView(R.id.ll_crate_corps)
    LinearLayout llCrateCorps;
    @BindView(R.id.ll_add_corps)
    LinearLayout llAddCorps;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person_challenge;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("开启挑战");
        vTitleLine.setVisibility(View.GONE);
    }


    @OnClick({R.id.top_btn_left, R.id.ll_freedom_challenge, R.id.ll_crate_corps, R.id.ll_add_corps})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.ll_freedom_challenge:
                onFreedomChallenge();
                break;
            case R.id.ll_crate_corps:
                startActivity(new Intent(this, CrateCorpsActivity.class));
                break;
            case R.id.ll_add_corps:
                Intent intent = new Intent(this, AddTeamActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 自由挑战
     */
    private void onFreedomChallenge() {
        new ChallengeAPI().onFreedomChallenge(new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {
                if (response.getCode() == 0) {
                    EventBus.getDefault().post(4);
                    startActivity(new Intent(PersonChallengeActivity.this, MainActivity.class));
                    finish();
                } else {
                    ToastUtils.showShort(response.getMessage());
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBusEntity event) {
        switch (event.getMsg()) {
            case 4:
                finish();
                break;
            default:
                break;
        }
    }
}